package com.xiaoxu.intranetweb.mybatisplusSzappdb.controller.statistics;

import com.alibaba.fastjson.JSONObject;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.SaleStatisticsBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.bo.YearSaleExcelBo;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.business.statistics.StatisticBu;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightSheets;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.impl.CWeightSheetsServiceImpl;
import com.xiaoxu.intranetweb.util.SzResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@RestController
@RequestMapping("/saleStatistic")
public class SaleStatisticsController {
    @Autowired
    private StatisticBu statisticBu;
    @Autowired
    private CWeightSheetsServiceImpl weightSheetsService;
    private String formatDateTime(String dateTimeStr) {
        // 定义日期时间格式
        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        try {
            // 尝试解析输入的日期时间字符串
            LocalDateTime dateTime = LocalDateTime.parse(dateTimeStr, inputFormatter);
            // 如果成功解析，则添加时分秒部分
            return dateTime.format(outputFormatter);
        } catch (Exception e) {
            // 如果解析失败（例如，已经包含时分秒），则直接返回原始字符串
            return dateTimeStr;
        }
    }
    @PostMapping("/saleList")
    public SzResult saleList(@RequestBody SaleStatisticsBo saleStatisticsBo, HttpServletRequest request){
//            private String startTime;//开始时间
//    private String endTime;//结束时间
        // 处理 startTime 和 endTime
        if (saleStatisticsBo.getStartTime() != null && !saleStatisticsBo.getStartTime().isEmpty()) {
            saleStatisticsBo.setStartTime(formatDateTime(saleStatisticsBo.getStartTime()));
        }
        if (saleStatisticsBo.getEndTime() != null && !saleStatisticsBo.getEndTime().isEmpty()) {
            saleStatisticsBo.setEndTime(formatDateTime(saleStatisticsBo.getEndTime()));
        }
        return weightSheetsService.saleList(saleStatisticsBo);
    }
    @PostMapping("/saleReceipts")
    public void saleReceipts(@RequestBody SaleStatisticsBo saleStatisticsBo, HttpServletRequest request, HttpServletResponse response){
        if (saleStatisticsBo.getStartTime() != null && !saleStatisticsBo.getStartTime().isEmpty()) {
            saleStatisticsBo.setStartTime(formatDateTime(saleStatisticsBo.getStartTime()));
        }
        if (saleStatisticsBo.getEndTime() != null && !saleStatisticsBo.getEndTime().isEmpty()) {
            saleStatisticsBo.setEndTime(formatDateTime(saleStatisticsBo.getEndTime()));
        }
        weightSheetsService.saleReceipts(saleStatisticsBo,response);
    }
    @PostMapping("/saleReceiptsAll")
    public void saleReceiptsAll(@RequestBody SaleStatisticsBo saleStatisticsBo, HttpServletRequest request, HttpServletResponse response){
        if (saleStatisticsBo.getStartTime() != null && !saleStatisticsBo.getStartTime().isEmpty()) {
            saleStatisticsBo.setStartTime(formatDateTime(saleStatisticsBo.getStartTime()));
        }
        if (saleStatisticsBo.getEndTime() != null && !saleStatisticsBo.getEndTime().isEmpty()) {
            saleStatisticsBo.setEndTime(formatDateTime(saleStatisticsBo.getEndTime()));
        }
        weightSheetsService.saleReceiptsAll(saleStatisticsBo,response);
    }
    @PostMapping("/yearSaleList")
    public SzResult yearSaleList(@RequestBody YearSaleExcelBo yearSaleExcelBo, HttpServletRequest request){
        if (yearSaleExcelBo.getStartTime() != null && !yearSaleExcelBo.getStartTime().isEmpty()) {
            yearSaleExcelBo.setStartTime(formatDateTime(yearSaleExcelBo.getStartTime()));
        }
        if (yearSaleExcelBo.getEndTime() != null && !yearSaleExcelBo.getEndTime().isEmpty()) {
            yearSaleExcelBo.setEndTime(formatDateTime(yearSaleExcelBo.getEndTime()));
        }
        try {
            return weightSheetsService.yearSaleList(yearSaleExcelBo);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/yearSaleExcel")
    public void yearSaleExcel(@RequestBody YearSaleExcelBo yearSaleExcelBo, HttpServletRequest request, HttpServletResponse response){
        if (yearSaleExcelBo.getStartTime() != null && !yearSaleExcelBo.getStartTime().isEmpty()) {
            yearSaleExcelBo.setStartTime(formatDateTime(yearSaleExcelBo.getStartTime()));
        }
        if (yearSaleExcelBo.getEndTime() != null && !yearSaleExcelBo.getEndTime().isEmpty()) {
            yearSaleExcelBo.setEndTime(formatDateTime(yearSaleExcelBo.getEndTime()));
        }
        try {
            weightSheetsService.yearSaleExcel(yearSaleExcelBo,response);
        } catch (IOException | ParseException e) {
            throw new RuntimeException(e);
        }
    }
    @PostMapping("/solidWasteExcel")
    public void solidWasteExcel(@RequestParam("file") MultipartFile file, HttpServletRequest request, HttpServletResponse response){
        weightSheetsService.solidWasteExcel(file,response);
    }
    @PostMapping("/solidWasteNowExcel")
    public void solidWasteExcel(@RequestBody JSONObject jsonObject , HttpServletResponse response){
        String startTime = jsonObject.getString("startTime");
        String endTime = jsonObject.getString("endTime");
        weightSheetsService.solidWasteNowExcel(startTime,endTime,response);
    }
}
